﻿// Copyright (C) Microsoft Corporation. All Rights Reserved.
// This code released under the terms of the
// Microsoft Limited Public License (Ms-LPL).

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;

using Microsoft.VisualStudio.IntelliTrace;
using Microsoft.VisualStudio.Samples.IHandlers;

namespace Microsoft.VisualStudio.Samples
{
	internal class ProcessChainFormatter : EventChainFormatter
	{

        IList<ProcessChainHandler> handlers;

        public ProcessChainFormatter(IntelliTraceProcess process)
			: base(process)
		{
            handlers = new List<ProcessChainHandler>();
		}
        
        public override void FireEvents()
        {
            if (handlers.Count > 0) base.FireEvents();
        }

		protected override void FireEvent(IntelliTraceEvent ev)
		{
            foreach (ProcessChainHandler handler in handlers)
            {
                if(ev.GetType() == typeof(CollectionPlanEvent) || ev.GetType().IsSubclassOf(typeof(CollectionPlanEvent))){
                    handler.CollectionPlanEvent(ev);
                } else if(ev.GetType() == typeof(SystemInformationEvent) || ev.GetType().IsSubclassOf(typeof(SystemInformationEvent))){
                    handler.SystemInformationEvent(ev);
                } else if(ev.GetType() == typeof(ModuleLoadEvent) || ev.GetType().IsSubclassOf(typeof(ModuleLoadEvent))){
                    handler.ModuleLoadEvent(ev);
                } else if(ev.GetType() == typeof(ModuleUnloadEvent) || ev.GetType().IsSubclassOf(typeof(ModuleUnloadEvent))){
                    handler.ModuleUnloadEvent(ev);
                } else if(ev.GetType() == typeof(ThreadCreateEvent) || ev.GetType().IsSubclassOf(typeof(ThreadCreateEvent))){
                    handler.ThreadCreateEvent(ev);
                } else if(ev.GetType() == typeof(ThreadDestroyEvent) || ev.GetType().IsSubclassOf(typeof(ThreadDestroyEvent))){
                    handler.ThreadDestroyEvent(ev);
                } else if(ev.GetType() == typeof(ThreadNameChangedEvent) || ev.GetType().IsSubclassOf(typeof(ThreadNameChangedEvent))){
                    handler.ThreadNameChangedEvent(ev);
                } else if(ev.GetType() == typeof(ProcessDestroyedEvent) || ev.GetType().IsSubclassOf(typeof(ProcessDestroyedEvent))){
                    handler.ProcessDestroyedEvent(ev);
                } else if(ev.GetType() == typeof(ModuleTokenEvent) || ev.GetType().IsSubclassOf(typeof(ModuleTokenEvent))){
                    handler.ModuleTokenEvent(ev);
                } else if(ev.GetType() == typeof(ModuleMetadataEvent) || ev.GetType().IsSubclassOf(typeof(ModuleMetadataEvent))){
                    handler.ModuleMetadataEvent(ev);
                } else if(ev.GetType() == typeof(ProcessInformationEvent) || ev.GetType().IsSubclassOf(typeof(ProcessInformationEvent))){
                    handler.ProcessInformationEvent(ev);
                }
            }
		}

        protected override StreamChain getStreamChain()
        {
            return Process.CreateProcessChain<ProcessStreamChain>();
        }

        public void AddHandler(ProcessChainHandler handler)
        {
            handlers.Add(handler);
        }

    }
}
